package cn.chenc.framework.core.manage;

import jakarta.annotation.PreDestroy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;



/**
 * 确保应用退出时能关闭后台线程
 *
 * @author secret
 */
public class ShutdownManager {
    private static final Logger logger = LoggerFactory.getLogger(ShutdownManager.class);

    private AsyncManager asyncManager;

    public ShutdownManager(AsyncManager asyncManager) {
        this.asyncManager = asyncManager;
    }

    @PreDestroy
    public void destroy() {
        shutdownAsyncManager();
    }

    /**
     * 停止异步执行任务
     */
    private void shutdownAsyncManager() {
        try {
            if(asyncManager!=null){
                logger.info("====关闭后台任务任务线程池====");
                asyncManager.shutdown();
            }
        }
        catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }
}
